<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  路由的pathList
  <script>
    const routes = [
      {
        "path": "/",
        "component": 'Layout',
        "hidden": true,
        "children": [

        ]
      },
      {
        "path": "/system",
        "name": "System",
        "meta": {
          "title": "系统管理",
          "icon": "el-icon-setting"
        },
        "component": 'Layout',
        "hidden": false,
        "children": [
          {
            "path": "menus",
            "component": "system/menus",
            "hidden": false,
            "meta": {
              "title": "菜单管理",
              "icon": "el-icon-menu",
            },
            "children": [
              {
                "path": "/menus/menu-1",
                "component": "system/menus/menu-1",
                "hidden": true,
                "meta": {
                  "title": "菜单menu-1",
                  "icon": "el-icon-menu",
                }
              }
            ]
          },
          {
            "path": "role",
            "component": "system/role",
            "hidden": false,
            "meta": {
              "title": "角色管理",
              "icon": "el-icon-role",
            },
            "children": [
              {
                "path": "detail",
                "component": "system/role/detail",
                "hidden": false,
                "meta": {
                  "title": "role详情页",
                  "icon": "el-icon-menu",
                }
              }
            ]
          }
        ]
      },
      {
        "path": "/mode",
        "name": "Mode",
        "meta": {
          "title": "模型管理"
        },
        "component": 'Layout',
        "hidden": false,
      }
    ];
    function resolve(...paths) {
      let resolvePath = '';
      let isAbsolutePath = false;
      for (let i = paths.length - 1; i > -1; i--) {
        let path = paths[i];
        if (isAbsolutePath) {
          break;
        }
        if (!path) {
          continue
        }
        resolvePath = path + '/' + resolvePath;
        isAbsolutePath = path.charCodeAt(0) === 47;
      }
      if (/^\/+$/.test(resolvePath)) {
        resolvePath = resolvePath.replace(/(\/+)/, '/')
      } else {
        resolvePath = resolvePath.replace(/(?!^)\w+\/+\.{2}\//g, '')
          .replace(/(?!^)\.\//g, '')
          .replace(/\/+$/, '')
      }
      return resolvePath;
    }
    console.log(resolve('/aa', '../bb', 'cc', 'dd', '/'))
    console.log(resolve('/', '/system', 'user', '', 'userIndex'))

    function getPathList(routes, parentPaths = []) {
      let pathList = [];
      routes.forEach(route => {
        let paths = parentPaths.concat(route.path)
        if (route.children && route.children.length > 0) {
          pathList = pathList.concat(getPathList(route.children, paths))
        } else {
          pathList.push(paths)
        }
      })
      return pathList;
    }

    function parseAllPaths(routes) {
      let allPath = [];
      const pathList = getPathList(routes);
      pathList.forEach(paths => {
        allPath.push(resolve.apply(null, paths))
      })
      return allPath;
    }

    function getAllMatches(routes, parentRoutes = []) {
      let allMatches = [];
      routes.forEach(route => {
        let matches = parentRoutes.concat(route)
        if (route.children && route.children.length > 0) {
          allMatches = allMatches.concat(getAllMatches(route.children, matches))
        } else {
          allMatches.push(matches)
        }
      })
      return allMatches;
    }

    function getRouteMatchesByPath(routes, path, parentRoutes = [], root = true) {
      let allMatches = getAllMatches(routes);
      for (let i = 0; i < allMatches.length; i++) {
        let matches = allMatches[i];
        let curPath = resolve(...matches.map(route => route.path));
        if (curPath === path) {
          return matches;
        }
      }
    }

    // function getCurRouteMatches(routes, route){

    // }


    console.log(getPathList(routes))
    console.log(parseAllPaths(routes));
    console.log(getAllMatches(routes));

    console.log(getRouteMatchesByPath(routes, '/system/role'))
    console.log(getRouteMatchesByPath(routes, '/menus/menu-1'))
  </script>
</body>

</html>